package com.cloud.www.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.www.model.domain.UserInfo;

import com.cloud.www.model.entity.MFilm;

import com.cloud.www.model.entity.MFilmDetail;
import com.cloud.www.service.MFilmDetailService;
import com.cloud.www.service.MFilmService;
import com.cloud.www.util.BlogUtils;
import com.cloud.www.util.PageHelper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//import cn.hutool.db.Page;

@Controller
@AllArgsConstructor
@RequestMapping("")
public class DetailController extends BaseController {

    private final MFilmService filmService;
    private final MFilmDetailService mFilmDetailService;

    /**
     * 详情
     */
    @GetMapping("info")
    public ModelAndView info(HttpServletRequest request, HttpServletResponse response) {
        Integer id = Convert.toInt(request.getParameter("id"), 0);
        MFilm data = filmService.getById(id);
        if (null == data) {
            try {
                response.sendRedirect("/");
            } catch (IOException e) {
            }
            return null;
        }
        ModelAndView view = baseView(data.getName() + "-成长社区", "detail/info");
        view.addObject("keyword", "成长社区," + data.getName());
        StringBuilder navTop = new StringBuilder();
        navTop.append(" <a href=\"/o_" + data.getOneTypeId() + ".html\">" + data.getOneTypeName() + "</a>&nbsp;&nbsp;»&nbsp;&nbsp;");
        navTop.append("<a href=\"/t_" + data.getTwoTypeId() + ".html\">" + data.getTwoTypeName() + "</a>");

        view.addObject("topNav", navTop.toString());


        String performer = data.getPerformer();
        if (StrUtil.isNotEmpty(performer)) {
            performer = StrUtil.replace(performer, ",", " ");
            performer = StrUtil.replace(performer, "，", " ");
            String[] listPerformer = StrUtil.split(performer, " ");

            StringBuilder sbPerformer = new StringBuilder("主演：");
            for (String item : listPerformer) {
                sbPerformer.append("<a href=\"/search.html?kw=" + item + "\">" + item + "</a> ");
            }
            view.addObject("performer", sbPerformer.toString());
        }

        String typeUrl = "";
        Page pRecommonend = new Page();
        pRecommonend.setSize(12);
        pRecommonend.setCurrent(1);
        List<MFilm> listRecommend = null;
        if (data.getTwoTypeId() != null) {
            typeUrl = "类型：<a href=\"/t_" + data.getTwoTypeId() + ".html\">" + data.getTwoTypeId() + "</a>";
            //二级分类推荐
            IPage<MFilm> pageRecommend = filmService.selectPage(pRecommonend, new MFilm().setTwoTypeId(data.getTwoTypeId()));
            listRecommend = pageRecommend.getRecords();
        } else if (data.getOneTypeId() != null) {
            typeUrl = "类型：<a href=\"/o_" + data.getOneTypeId() + ".html\">" + data.getOneTypeId() + "</a>";
            //一级分类推荐
            IPage<MFilm> pageRecommend = filmService.selectPage(pRecommonend, new MFilm().setOneTypeId(data.getOneTypeId()));
            listRecommend = pageRecommend.getRecords();
        }
        view.addObject("listRecommend", listRecommend);
        view.addObject("typeUrl", typeUrl);

        List<MFilmDetail> listFilmDetail = mFilmDetailService.selectAll(new MFilmDetail().setFId(id));
        StringBuilder sbPlayData = new StringBuilder();
        for (
                MFilmDetail mFilmDetail : listFilmDetail) {
            String[] zykArray = StrUtil.split(mFilmDetail.getZyk(), "$");
            String name = zykArray.length > 0 ? zykArray[0] : "";
            sbPlayData.append("<li ><a title=\"" + name + "\" href=\"/play/" + mFilmDetail.getId() + ".html\" target=\"_self\">" + name + "</a></li>");
        }
        view.addObject("playData", sbPlayData.toString());
        view.addObject("data", data);
        return view;
    }

}
